import xph from '@/common/xph.js'
export default {
	namespaced: true,
    state: {
		// [{
		//  "id":9,
		// 	"createTime": "2021-09-10 10:03:33",
		// 	"updateTime": "2021-09-10 10:03:33",
		// 	"userId": 2111,
		// 	"name": "测试1组",
		// 	"description": "测试",
		// 	"mode": true,
		// 	"startTime": "2019-11-12 00:00:00",
		// 	"stopTime": "2019-11-12 23:59:59",
		// 	"duration": 20,
		// 	"interval": 21,
		// 	"circulation": 1,
		// 	"weekday": "1/2/3/4/5",
		// 	"groups": [{
		// 		"id": 37,
		// 		"createTime": "2021-09-10 09:59:34",
		// 		"updateTime": "2021-09-10 09:59:34",
		// 		"userId": 2111,
		// 		"name": "1号组",
		// 		"description": "测试",
		// 		"rotationRelays": [{
		// 			"id": 604,
		// 			"createTime": "2021-09-10 09:59:57",
		// 			"updateTime": "2021-09-10 09:59:57",
		// 			"groupId": 37,
		// 			"facId": 16069132,
		// 			"relay": 1
		// 		}]
		// 	}]
		// }]
		irrigations: [],
		
		// [{
		// 	"id": 37,
		// 	"createTime": "2021-09-10 09:59:34",
		// 	"updateTime": "2021-09-10 09:59:34",
		// 	"userId": 2111,
		// 	"name": "1号组",
		// 	"description": "测试",
		// 	"rotationRelays": [{
		// 		"id": 604,
		// 		"createTime": "2021-09-10 09:59:57",
		// 		"updateTime": "2021-09-10 09:59:57",
		// 		"groupId": 37,
		// 		"facId": 16069132,
		// 		"relay": 1
		// 	}
		groups: []
	},
    getters: {

    },
    mutations: {
		setIrrigationInfo(state, irrigations){
			state.irrigations = irrigations
		},
		setGroupInfo(state, groups){
			state.groups = groups
		},
		setIrrigationRelays(state){
			state.irrigations.map(irri => {
				irri.groups.map(igroup => {
					let group = state.groups.find(function(gro){
						return gro.id === igroup.id
					})
					igroup.rotationRelays = group.rotationRelays
				})
			})
		},
		setIrrigationRelay(state, param){
			param.valve.state = param.state
		},
		setIrrigationRelayLoading(state, param){
			param.valve.loading = param.state
		},
		setIrrigationReal(state, param){
			param.sensor.value = param.value
		}
    },
    actions: { 
		setIrrigationInfo(context, user){
			xph.getRotationConfig(user.userId).then(res => {
				context.commit('setIrrigationInfo', res)
				context.dispatch('setGroupInfo', user.username)
			})
		},
		setGroupInfo(context, username){
			xph.getRotationGroup(username).then(res => {
				context.commit('setGroupInfo', res)
				context.commit('setIrrigationRelays')
			})
		}
    }
}